package com.timepies.domain;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckedTextView;
import android.widget.TextView;

import com.timepies.DettaglioPieActivity;
import com.timepies.R;

public class PieAdapterExpandable extends BaseExpandableListAdapter {

	private List<Pie> list;
	public LayoutInflater inflater;
	public DettaglioPieActivity activity;

	public PieAdapterExpandable(DettaglioPieActivity activity, List<Pie> list) {
		this.activity = activity;
		this.list = list;
		inflater = activity.getLayoutInflater();
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		Pie pie = list.get(groupPosition);
		return activity.getInterval().printTxtSentence(pie, true);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return 0;
	}

	@Override
	public View getChildView(int groupPosition, final int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		final String children = (String) getChild(groupPosition, childPosition);
		TextView text = null;
		if (convertView == null) {
			convertView = inflater.inflate(
					R.layout.activity_pie_dettaglio_riga, null);
		}
		text = (TextView) convertView.findViewById(R.id.textView1);
		text.setText(children);
		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				activity.onClickShareInfo(v, children);
			}
		});
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return 1; // questa implementazione puo avere un solo figlio
	}

	@Override
	public Object getGroup(int groupPosition) {
		return list.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return list.size();
	}

	@Override
	public void onGroupCollapsed(int groupPosition) {
		super.onGroupCollapsed(groupPosition);
	}

	@Override
	public void onGroupExpanded(int groupPosition) {
		super.onGroupExpanded(groupPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return 0;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(
					R.layout.activity_pie_dettaglio_gruppo, null);
		}
		Pie group = (Pie) getGroup(groupPosition);
		((CheckedTextView) convertView).setText(group.getCategoria());
		((CheckedTextView) convertView).setChecked(isExpanded);

		// ----- GESTIONE ICONA DINAMICA -----
		// Bitmap bitmap = getBitmapFromURL("http://www.gravatar.com/avatar/"
		// + MD5Util.md5Hex(group.getCategoria()) + "?d=identicon&s=50");
		// BitmapDrawable img = new BitmapDrawable(bitmap);
		// ((CheckedTextView) convertView).setCompoundDrawables(
		// img, null, null, null);

		return convertView;
	}

	public static Bitmap getBitmapFromURL(String src) {
		try {
			Log.e("src", src);
			URL url = new URL(src);
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			connection.setDoInput(true);
			connection.connect();
			InputStream input = connection.getInputStream();
			Bitmap myBitmap = BitmapFactory.decodeStream(input);
			Log.e("Bitmap", "returned");
			return myBitmap;
		} catch (IOException e) {
			e.printStackTrace();
			Log.e("Exception", e.getMessage());
			return null;
		}
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}
}